<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2019/11/11
 * Time: 10:13
 */

namespace business\company\repositories;

class SupplierRepository
{
    /**
     * 获取某个时间后的供应商总数
     * @param $interval string 距今相差天数
     * @return int
     * @throws \yii\base\InvalidConfigException
     */
    public function getSupplierCountByDate($interval = null)
    {
        /** @var Connection $connection */
        $connection = \Yii::$app->get('db');

        $sql = "SELECT count(1) FROM b2b_company.uuc_supplier_basic WHERE 1=1";
        if (!empty($interval)) {
            $sql .= " AND last_updated_time > :datetime";
            return $connection->createCommand($sql)
                ->bindValue(":datetime", date("Y-m-d", strtotime($interval . " day")))
                ->queryScalar();
        } else {
//            $sql .= " AND status = 2 AND state = 2";
            return $connection->createCommand($sql)->queryScalar();
        }
    }

    /**
     * 获取某个时间后的供应商总数
     * @param $startDate
     * @param $endDate
     * @return mixed
     * @throws \yii\base\InvalidConfigException
     */
    public function getSupplierCountByTimeSlot($startDate, $endDate)
    {
        /** @var Connection $connection */
        $connection = \Yii::$app->get('db');

        $sql = "SELECT count(1) FROM b2b_company.uuc_supplier_basic WHERE 1=1";
        $sql .= " AND last_updated_time > :startdatetime AND last_updated_time < :enddatetime";
        return $connection->createCommand($sql)
            ->bindValue(':startdatetime', $startDate)
            ->bindValue(':enddatetime', $endDate)
            ->queryScalar();
    }

    /**
     * 从供应商表中获取变更的供应商id
     * @param $offset
     * @param $pageSize
     * @return array
     */
    public function getAllSupplierId($offset, $pageSize)
    {
        $sql = <<<sql
            SELECT supplier_id
            FROM b2b_company.uuc_supplier_basic 
            WHERE supplier_id > :offset/* AND status = 2 AND state = 2*/
            order by supplier_id ASC
            LIMIT :pageSize
sql;
        /** @var Connection $connection */
        $connection = \Yii::$app->get('db');
        $result = $connection->createCommand($sql)
            ->bindValue(':offset', $offset)
            ->bindValue(':pageSize', $pageSize)
            ->queryColumn();
        return $result;
    }

    /**
     * 从大数据表中获取变更的供应商id
     * @param $interval
     * @param $offset
     * @param $pageSize
     * @return array
     */
    public function getChangedSupplierInfo($interval, $offset, $pageSize)
    {
        $sql = <<<sql
            SELECT supplier_id
            FROM b2b_company.uuc_supplier_basic 
            WHERE supplier_id > :offset AND last_updated_time > :datetime/* AND status = 2 AND state = 2*/
            order by supplier_id ASC
            LIMIT :pageSize
sql;
        /** @var Connection $connection */
        $connection = \Yii::$app->get('db');
        $result = $connection->createCommand($sql)
            ->bindValue(':datetime', date("Y-m-d", strtotime($interval . " day")))
            ->bindValue(':offset', $offset)
            ->bindValue(':pageSize', $pageSize)
            ->queryColumn();
        return $result;
    }

    /**
     * 从大数据表中获取变更的供应商id
     * @param $startDate
     * @param $endDate
     * @param $offset
     * @param $pageSize
     * @return mixed
     * @throws \yii\base\InvalidConfigException
     */
    public function getChangedSupplierInfoByTimeSlot($startDate, $endDate, $offset, $pageSize)
    {
        $sql = <<<sql
            SELECT supplier_id
            FROM b2b_company.uuc_supplier_basic 
            WHERE supplier_id > :offset AND last_updated_time > :startdatetime AND last_updated_time < :enddatetime/* AND status = 2 AND state = 2*/
            order by supplier_id ASC
            LIMIT :pageSize
sql;
        /** @var Connection $connection */
        $connection = \Yii::$app->get('db');
        $result = $connection->createCommand($sql)
            ->bindValue(':startdatetime', $startDate)
            ->bindValue(':enddatetime', $endDate)
            ->bindValue(':offset', $offset)
            ->bindValue(':pageSize', $pageSize)
            ->queryColumn();
        return $result;
    }

    /**
     * 根据供应商id获取供应商基本信息
     * @param array $supplierId
     * @return array|false
     */
    public function getSupplierBasicInfoByIds(array $supplierId)
    {
        $sql = <<<sql
            SELECT 
                a.supplier_id AS supplier_id
            FROM
                b2b_company.uuc_supplier_basic a
sql;
//        AND status = 2 AND state = 2
        $condition = ' WHERE a.supplier_id in ("' . implode('","', $supplierId) . '")';
        /** @var Connection $connection */
        $connection = \Yii::$app->get('db');
        $result = $connection->createCommand($sql . $condition)
            ->queryAll();
        return $result;
    }

    /**
     * 通过公司名称返回供应商信息
     * @param string $companyName
     * @return mixed
     * @throws \yii\base\InvalidConfigException
     */
    public function getSupplierBasicInfoByCompanyName($companyName = '')
    {
        $sql = <<<sql
                  select 
                      supplier_id, company_name
                  from 
                      b2b_company.uuc_supplier_basic 
                  where company_name = :company_name
sql;
        return \Yii::$app->get('db')
            ->createCommand($sql)
            ->bindValue(':company_name', $companyName)
            ->queryOne();
    }
}
